package com.ekuaitu.kuaitustaff.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.ekuaitu.kuaitustaff.R;
import com.ekuaitu.kuaitustaff.baiduMap.GeoCoderUtils;
import com.ekuaitu.kuaitustaff.baiduMap.MapStatusUtils;
import com.ekuaitu.kuaitustaff.base.BaseActivity;
import com.ekuaitu.kuaitustaff.utils.AppManager;
import com.ekuaitu.kuaitustaff.utils.IntentKey;
import com.ekuaitu.kuaitustaff.utils.Logger;
import com.ekuaitu.kuaitustaff.utils.SPKey;
import com.ekuaitu.kuaitustaff.utils.SPUtils;
import com.ekuaitu.kuaitustaff.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class DebugModeActivity extends BaseActivity {

    @BindView(R.id.debug_mapView)
    MapView debugMapView;
    @BindView(R.id.dev_mode)
    RadioButton devMode;
    @BindView(R.id.dev2_mode)
    RadioButton dev2Mode;
    @BindView(R.id.test_mode)
    RadioButton testMode;
    @BindView(R.id.prod_mode)
    RadioButton prodMode;
    @BindView(R.id.local_mode)
    RadioButton localMode;
    @BindView(R.id.auto_location)
    RadioButton autoLocation;
    @BindView(R.id.fixed_location)
    RadioButton fixedLocation;
    @BindView(R.id.webviewUrl)
    EditText webviewUrl;
    private BaiduMap mBaiduMap;

    private Context mContext = this;

    @Override
    public int getLayoutId() {
        return R.layout.activity_debug_mode;
    }

    @Override
    public void initView() {
        initMap();
        switch (SPUtils.getInstance(mContext).getIntValue(SPKey.URLMODE)) {
            case 1:
                devMode.setChecked(true);
                urlMode = 1;
                break;
            case 2:
                testMode.setChecked(true);
                urlMode = 2;
                break;
            case 3:
                prodMode.setChecked(true);
                urlMode = 3;
                break;
            case 4:
                dev2Mode.setChecked(true);
                urlMode = 4;
                break;
            case 5:
                localMode.setChecked(true);
                urlMode = 5;
                break;
            default:
                testMode.setChecked(true);
                urlMode = 2;
                break;


        }


        switch (SPUtils.getInstance(mContext).getIntValue(SPKey.LOCATIONMOED)) {
            case 1:
                autoLocation.setChecked(true);
                locationMode = 1;
                break;
            case 2:
                fixedLocation.setChecked(true);
                locationMode = 2;
                break;
            default:
                autoLocation.setChecked(true);
                locationMode = 1;
                break;
        }

    }

    @Override
    protected void initData() {

    }

    //初始化地图
    public void initMap() {
        debugMapView.showZoomControls(true);
        //MapView.setMapCustomEnable(true);
        if (mBaiduMap == null) {
            mBaiduMap = debugMapView.getMap();
            // 开启定位图层
            mBaiduMap.setBuildingsEnabled(false);
            mBaiduMap.setMyLocationEnabled(true);
            mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
                    MyLocationConfiguration.LocationMode.NORMAL, true,
                    BitmapDescriptorFactory.fromResource(R.drawable.icon_mylocation), 0x00000000, 0x00000000));
            //设置地图交互
            UiSettings uiSettings = mBaiduMap.getUiSettings();
            uiSettings.setRotateGesturesEnabled(false);
            uiSettings.setOverlookingGesturesEnabled(false);
            uiSettings.setZoomGesturesEnabled(true);
            if (SPUtils.getInstance(mContext).getIntValue(SPKey.LOCATIONMOED) == 2) {
                LatLng storagePosition = new LatLng(
                        Double.parseDouble(SPUtils.getInstance(mContext).getStringValue(SPKey.FIX_LATITUDE)),
                        Double.parseDouble(SPUtils.getInstance(mContext).getStringValue(SPKey.FIX_LONGITUDE)));
                new MapStatusUtils(mBaiduMap, mContext).animateMove(storagePosition, 16, 400);
            }
        }
    }

    private int urlMode;
    private int locationMode;

    @OnClick({R.id.dev_mode, R.id.dev2_mode, R.id.test_mode, R.id.prod_mode, R.id.local_mode, R.id.auto_location, R.id.fixed_location, R.id.apply_config, R.id.webview_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dev_mode:
                devMode.setChecked(true);
                urlMode = 1;
                break;

            case R.id.dev2_mode:
                dev2Mode.setChecked(true);
                urlMode = 4;
                break;
            case R.id.test_mode:
                testMode.setChecked(true);
                urlMode = 2;
                break;
            case R.id.prod_mode:
                prodMode.setChecked(true);
                urlMode = 3;
                break;
            case R.id.local_mode:
                localMode.setChecked(true);
                urlMode = 5;
                break;
            case R.id.auto_location:
                autoLocation.setChecked(true);
                locationMode = 1;
                break;
            case R.id.fixed_location:
                locationMode = 2;
                fixedLocation.setChecked(true);
                break;
            case R.id.apply_config:
                showLoadingDialog();
                if (locationMode == 2) {//手动
                    GeoCoderUtils.getInstance().reverseGeoCode(mBaiduMap.getMapStatus().target, new GeoCoderUtils.OnGeoListener() {
                        @Override
                        public void onGeoSuccess(String address, String cityCode, String city, String adCode) {
                            Logger.i(cityCode + "地方广东省");
                            SPUtils.getInstance(mContext).putStringValue(SPKey.AREA_CODE, cityCode);
                            SPUtils.getInstance(mContext).putStringValue(SPKey.AD_CODE, adCode);
                            SPUtils.getInstance(mContext).putStringValue(SPKey.CITY, city);
                            SPUtils.getInstance(mContext).putStringValue(SPKey.FIX_LATITUDE, mBaiduMap.getMapStatus().target.latitude + "");
                            SPUtils.getInstance(mContext).putStringValue(SPKey.FIX_LONGITUDE, mBaiduMap.getMapStatus().target.longitude + "");
                            dismissLoadingDialog();
                            ToastUtils.makeText(mContext, "应用成功，重启生效", ToastUtils.LENGTH_LONG).show();
                            SPUtils.getInstance(mContext).putIntValue(SPKey.URLMODE, urlMode);
                            SPUtils.getInstance(mContext).putIntValue(SPKey.LOCATIONMOED, locationMode);
                            SPUtils.getInstance(mContext).clearSharePreference();

                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    AppManager.getAppManager().AppExit(mContext);
                                }
                            }, 2000);
                        }

                        @Override
                        public void onGeoFailure() {
                            dismissLoadingDialog();
                            ToastUtils.makeText(mContext, "设置失败，请重试", ToastUtils.LENGTH_SHORT).show();
                        }
                    });

                } else {
                    dismissLoadingDialog();
                    ToastUtils.makeText(mContext, "应用成功，重启生效", ToastUtils.LENGTH_LONG).show();
                    SPUtils.getInstance(mContext).putIntValue(SPKey.URLMODE, urlMode);
                    SPUtils.getInstance(mContext).putIntValue(SPKey.LOCATIONMOED, locationMode);
                    SPUtils.getInstance(mContext).clearSharePreference();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            AppManager.getAppManager().AppExit(mContext);
                        }
                    }, 2000);
                    Logger.i(SPKey.URLMODE, SPUtils.getInstance(mContext).getIntValue(SPKey.URLMODE) + "");
                }
                break;
            case R.id.webview_enter:
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(IntentKey.INTENT_WEBVIEW_URL, webviewUrl.getText().toString());
                startActivity(intent);
                break;
        }
    }

}
